import VueRouter from 'vue-router'
import Vue from 'vue'
import auth from '@/utils/auth'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: () => import('../views/login/login.vue')
  },
  {
    path: '/',
    component: () => import('../views/login/layout.vue'),
    children: [
      {
        path: '',
        component: () => import('../home/index.vue')

      },
      {
        path: '/article',
        component: () => import('../views/login/article/index.vue')
      },
      {
        path: '/publish',
        component: () => import('../views/login/publish/index.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const token = auth.getToken()
  if (to.path !== '/login' && !token) return next('/login')
  next()
})

export default router
